<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\MySubject;
use builder\Builder;
use think\Db;
use think\Request;

class Huisou
{
    public function show_list(Request $request){
        return Builder::table()
            ->setAction('huisou/page')
            ->addToolbarDemo('alldel','全部删除','','huisou/qudel')
            ->addToolbarDemo('alldel','全部恢复','','huisou/quhuifu','','background-color: #00b7ee')
            ->setColumn('input','name','名称')
            ->setColumn('input','cid_name','栏目')
            ->setColumn('input','model','表名称')
            ->setRightHeader()
            ->setButton('del','恢复','huisou/huifu','','background-color: #00b7ee')
            ->setButton('del','删除','huisou/del')
            ->setPage()
            ->fetch();
    }
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $page = $request->param('page',1);
        $model = Db::name(getTable('models'))
            ->where('type','单表')
            ->select();
        $data = [];
        foreach ($model as $value){
            $array = Db::name($value['table'])
                ->where('delete_time',1)
                ->select();
            if($array){
                $data = array_merge($data,$array);
            }
        }
        $res['total'] = count($data);
        $data = array_slice($data,($page-1)*$limit,$limit);
        foreach ($data as $key=>$value){
            $cid = Db::name(getTable('column'))->where('id',$value['cid'])
                ->find();
            if($cid){
                $value['cid_name'] = $cid['name'];
                $model = Db::name(getTable('models'))->where('id',$cid['model_id'])->find();
                if($model){
                    $value['model'] = $model['name'];
                }
            }
            $data[$key] = $value;
        }

        $res['data'] = $data;
//        $res = Db::name('index_danbiao')
//            ->where('delete_time',1)
//            ->paginate(10)
//            ->toArray();
        return json_page_response($res);
    }
    public function huifu(Request $request){
        $data  = $request->param('data');
        $obj = new MySubject();
        $obj->setCid($data['cid']);
        $table = $obj->getModel('table');
        if(Db::name($table)->where('id',$data['id'])->update(['delete_time'=>null])){
            return json_to_respos('恢复成功');
        }else{
            return json_to_respos('恢复失败',205);
        }
    }
    public function del(Request $request){
        $data  = $request->param('data');
        $obj = new MySubject();
        $obj->setCid($data['cid']);
        $table = $obj->getModel('table');
        if(Db::name($table)->where('id',$data['id'])->delete()){
            return json_to_respos('删除成功');
        }else{
            return json_to_respos('删除失败',205);
        }
    }
    public function quhuifu(Request $request){
        $data = $request->param('data');
        $obj = new MySubject();
        foreach ($data as $value){
            $obj->setCid($value['cid']);
            $table = $obj->getModel('table');
            if($table){
                Db::name($table)->where('id',$value['id'])->update(['delete_time'=>null]);
            }
        }
        return json_to_respos('恢复成功');
    }
    public function qudel(Request $request){
        $data = $request->param('data');
        $obj = new MySubject();
        foreach ($data as $value){
            $obj->setCid($value['cid']);
            $table = $obj->getModel('table');
            if($table){
                Db::name($table)->where('id',$value['id'])->delete();
            }
        }
        return json_to_respos('删除成功');
    }
}